﻿import kliment.math.Calc;
import kliment.geom.color.*;

/**
 * ...
 * @author	Kliment
 * @version	1.0.0
*/
class kliment.geom.color.CMYKColor extends ColorAbstract {
	private var WHITE:Number = 100;
	
	public var rawM:Number;
	public var rawK:Number;
	public var rawY:Number;
	public var rawC:Number;
	
	function CMYKColor(host:Color) {
		super(host);
	}
	
	public function get c():Number { return rawC * WHITE }
	public function get m():Number { return rawM * WHITE }
	public function get y():Number { return rawY * WHITE }
	public function get k():Number { return rawK * WHITE }
	
	public function setValue(c:Number, m:Number, y:Number, k:Number, update:Boolean):Void {
		setRawValue(c / WHITE, m / WHITE, y / WHITE, k / WHITE, update);
	}
	
	public function setRawValue(c:Number, m:Number, y:Number, k:Number, update:Boolean):Void {
		if (update == undefined) update = true;
		if (rawC == c && rawM == m && rawY == y && rawK == k)
			return;
		
		rawC = Calc.inInterval(MIN_RAW, MAX_RAW, c);
		rawM = Calc.inInterval(MIN_RAW, MAX_RAW, m);
		rawY = Calc.inInterval(MIN_RAW, MAX_RAW, y);
		rawK = Calc.inInterval(MIN_RAW, MAX_RAW, k);
		
		if (color && update)
			color.valueChanged(Color.CMYK);
	}
	
	public function toString():String {
		return "[CMYKColor c=" + c + " m=" + m + " y=" + y + " k=" + k +"]";
	}
	
	public function clone():CMYKColor {
		var result:CMYKColor = new CMYKColor();
		result.setRawValue(rawC, rawM, rawY, rawK, false);
		return result;
	}
	
	public function toRGB(rgb:RGBColor):RGBColor { return converToRGB(this, rgb) }
	static public function converToRGB(cmyk:CMYKColor, rgb:RGBColor):RGBColor {
		if (!rgb) rgb = new RGBColor();
		var rawK:Number = cmyk.rawK;
		var _rawK:Number = 1 - rawK;
		rgb.setRawValue(1 - (cmyk.rawC * _rawK + rawK), 1 - (cmyk.rawM * _rawK + rawK), 1 - (cmyk.rawY * _rawK + rawK), false);
		return rgb;
	}
}